<template>
  <x-page>
    <x-filter @search="handleSearch" @reset="handleReset">
      <x-filter-item :label="'供应商编码/名称:'">
        <el-input v-model="searchParams.number" :placeholder="'请输入'" maxlength="100" clearable></el-input>
      </x-filter-item>
      <x-filter-item :label="'整改通知编号:'">
        <el-input v-model="searchParams.number" :placeholder="'请输入'" maxlength="100" clearable></el-input>
      </x-filter-item>

      <x-filter-item :label="'审批状态:'">
        <x-select-tree
          v-model="searchParams.deptId"
          ref="selectTree"
          :options="treeData"
          placeholder="请选择"
          :defaultProps="treeProps"
        />
      </x-filter-item>
      <x-filter-item :label="'创建人:'">
        <el-input v-model="searchParams.number" :placeholder="'请输入'" maxlength="100" clearable></el-input>
      </x-filter-item>

      <x-filter-item :label="'创建时间:'">
        <el-date-picker
          v-model="searchParams.createTime"
          type="daterange"
          value-format="yyyy-MM-dd"
          :range-separator="$t('common.to')"
          :start-placeholder="'开始时间'"
          :end-placeholder="'结束时间'"
        >
        </el-date-picker>
      </x-filter-item>
    </x-filter>
    <x-data>
      <!--数据面板按钮插糟-->
      <template #tool>
        <el-row type="flex" class="tools" justify="space-between">
          <el-col :span="20">
            <el-button type="primary" @click="handleAdd()">新增</el-button>
            <el-button @click="handleEmit()"> 发送供应商 </el-button>
            <el-button @click="handleExportExcel()" :icon="buttonIcons('export')"> 导出 </el-button>
          </el-col>
          <el-col :span="4" class="tools-config">
            <x-column-config :columns="columns" @updateColumns="updateColumns" @on-refresh="loadData" />
          </el-col>
        </el-row>
      </template>
      <!-- 数据表格 -->
      <x-table
        ref="tableCom"
        :pagination="pagination"
        :data-source="tableData"
        :loading="loading"
        :row-key="'id'"
        @on-change="handleTableChange"
        @on-selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column :label="$t('common.serialNumber')" :width="55" fixed type="index" />
        <el-table-column
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.prop"
          :fixed="item.fixed"
          :show-overflow-tooltip="true"
          :width="item.width"
          :min-width="item.minWidth || '-'"
          :label="item.label"
        >
        </el-table-column>
        // eslint-disable-next-line vue/no-parsing-error
        <el-table-column fixed="right" label="操作" :align="'center'" width="460">
          <template #default="scope">
            <el-button @click="handleDedailed(scope.row)" type="text" size="small">详情</el-button>
            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="handleDel(scope.row)" type="text" size="small" class="color">删除</el-button>
            <el-button @click="handleRevoke(scope.row)" type="text" size="small">撤回</el-button>
            <el-button @click="handleAudit(scope.row)" type="text" size="small">审批</el-button>
            <el-button @click="handleAuditLog(scope.row)" type="text" size="small">审批日志</el-button>
            <el-button @click="handleSend(scope.row)" type="text" size="small">发送</el-button>
            <el-button @click="handlePlan(scope.row)" type="text" size="small">审核方案</el-button>
            <el-button @click="handleResult(scope.row)" type="text" size="small">确认结果</el-button>
            <!-- <more-table-actions
              :permis-list="permissionsActions"
              :action-list="getActions(actions, scope.row)"
              :row="scope.row"
              @onActionClick="handleActions"
            /> -->
          </template>
        </el-table-column>
      </x-table>
    </x-data>
  </x-page>
</template>

<script>
  import { buttonIcons, PermissionsMixin, QueryDictMixin, TableListMixin, selectDictLabel } from '@package/utils'
  // eslint-disable-next-line import/named
  // import { getList } from '../api/supplierList.js'
  import { mapState } from 'pinia'
  import { columns, actions } from '@/modules/supplier/config/data.js'
  import { apiDeptTree } from '@/modules/config/api/index.js'
  import { EnumApiCode } from '../../../../../../globalEnum.js'
  import { pinias } from '@/common/pinia'
  // import { apiMaterialTypeTree } from '../api/supplierList.js'

  const { login, recordStore } = pinias
  export default {
    name: 'ManagePricing',
    mixins: [TableListMixin, QueryDictMixin, PermissionsMixin],
    data() {
      return {
        // getList,
        columns,
        actions,
        treeLoading: false,
        treeData: [],
        treeProps: {
          label: 'deptName',
          children: 'children'
        },
        searchParams: {
          number: '',
          deptId: '',
          state: '',
          type: '',
          createTime: '',
          agreementName: '',
          agreementCode: '',
          agreementType: '',
          startTime: [],
          beginTime: '',
          endTime: ''
        },
        // tableData:[],
        tableData: [{ num1: '1' }, { num2: '1' }, { num3: '1' }, { num4: '1' }, { num5: '1' }],
        srmDictKeys: ['srm_purchase_detail.state', 'srm_purchase.type']
      }
    },
    watch: {
      dictOptions: {
        handler(val) {
          console.log('val123', val)
        },
        deep: true,
        immediate: true
      }
    },
    // eslint-disable-next-line vue/order-in-components
    computed: {
      // eslint-disable-next-line no-undef
      ...mapState(recordStore, ['supplierId']),
      // eslint-disable-next-line no-undef
      ...mapState(login, ['user']),
      getActions() {
        return (actions) => {
          return actions.map((action) => {
            return action
          })
        }
      }
    },
    created() {
      this.loadTreeData() // 组织机构
    },
    mounted() {
      // this.loadTreeData() // 组织机构
      // this.getList()
    },
    methods: {
      // 详情
      handleDedailed() {},
      // 编辑
      handleEdit() {},
      // 删除
      handleDel() {},
      // 撤回
      handleRevoke() {},
      //  审批
      handleAudit() {},
      //  审批日志
      handleAuditLog() {},
      // 发送
      handleSend() {},
      // 审核方案
      handlePlan() {},
      // 确认结果
      handleResult() {},

      // eslint-disable-next-line no-undef
      // ...mapActions(recordStore, ['setSupplierId']),
      // async initSupplierDict() {
      //   try {
      //     // eslint-disable-next-line no-undef
      //     const data = await Promise.all([apiMaterialTypeTree(), apiSupplierRankList()])
      //     const { data: treeData } = data[0]
      //     const { data: levalData } = data[1]
      //     this.$set(this.dictOptions, 'access_type', treeData)
      //     this.$set(this.dictOptions, 'access_level', levalData)
      //   } catch {
      //     console.error('供应商等级/行业分类获取失败')
      //   }
      // },
      // 导出
      handleExportExcel() {
        // this.handleExport('test')
        console.log('导出')
        // 调用后端导出的接口
      },
      // 新增
      // handleAdd() {
      //   console.log('新增')
      // },
      /**
       * 新增
       *
       */
      handleAdd() {
        this.$router.push({ path: '/supplier/rectifyInform/addRectification' })
      },
      // 发送供应商
      handleEmit() {
        console.log('发送供应商')
      },
      // 详情按钮
      async handleActions({ key, data }) {
        const { id } = data
        switch (key) {
          case 'detail':
            // 跳转到供应商详情页面
            this.$router.push({
              name: 'recordDetail',
              // name: 'PurchaseApplyDetail',
              query: {
                id
              }
            })
            break
          default:
            break
        }
      },
      /**
       * 列表选中数据
       *
       */
      handleSelectionChange(data) {
        this.selectionData = data
      },
      // handleActions(action) {
      //   this.$emit('onActionClick', {
      //     key: action,
      //     data: { ...this.row }
      //   })
      // },
      buttonIcons,
      selectDictLabel,
      async loadTreeData() {
        this.treeLoading = true
        const { orgId } = this.user.mainPost
        // 获取组织机构
        const { data, code, msg } = await apiDeptTree({ orgId })
        this.treeLoading = false
        if (code === EnumApiCode.Success) {
          this.treeData = data
          console.log(this.treeData)
        } else {
          this.$message.error(msg)
        }
      },
      // 重置
      handleReset() {
        this.searchParams = {
          number: '',
          deptId: '',
          state: '',
          type: '',
          createTime: ''
        }
        this.resetSearch()
      },
      // 查询
      handleSearch(val) {
        const { number, state, deptId, type, createTime } = this.searchParams
        this.queryParam = {
          ...val,
          number,
          state,
          deptId,
          type,
          beginTime: createTime ? `${createTime[0]} 00:00:00` : '',
          endTime: createTime ? `${createTime[1]} 23:59:59` : ''
        }
        this.loadData()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .color {
    color: red;
  }
</style>
